﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorageLight.Core.Web
{
    /// <summary>
    /// ODataインタフェース用ファイルアイテム
    /// </summary>
    public class FileItem
    {
        [Key]
        public string Id { get; set; }

        [ReadOnly(true)]
        public string Type { get; set; }

        [ReadOnly(true)]
        public string Directory { get; set; }

        public string Title { get; set; }

        [ReadOnly(true)]
        public string SearchContent { get; set; }

        public string Owner { get; set; }

        [ReadOnly(true)]
        public string CheckoutUser { get; set; }

        [ReadOnly(true)]
        public DateTimeOffset? LastModified { get; set; }

        [ReadOnly(true)]
        public string Remark { get; set; }

        public string Path { get; set; }

        [ReadOnly(true)]
        public long Length { get; set; }

        public string OriginalUrl { get; set; }

        //[ReadOnly(true)]
        //public DateTimeOffset? FullTextEntryDate { get; set; }

        //[ReadOnly(true)]
        //public bool HasFilePasscode { get { return FileProtectedMode != "None"; } }

        [ReadOnly(true)]
        public string FileProtectedMode { get; set; }

        [ReadOnly(true)]
        public string AttachmentFileName
        {
            get 
            {
                var fileName = BlobItem.GetFileName(Path);
                if (FileProtectedMode == "Password" || FileProtectedMode == "UserPassword")
                {
                    if (MsOffCryptoUtil.IsOfficeFilePasscode(fileName)) return fileName;
                    if (fileName.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)) return fileName;
                    return fileName + ".zip";
                }
                if (FileProtectedMode == "ZipPassword" || FileProtectedMode == "ZipUserPassword")
                {
                    return fileName + ".zip";
                }
                if (FileProtectedMode == "RMS")
                {
                    return MsOffCryptoUtil.ConvertToRmsProtectedFileName(fileName);
                }

                return fileName;
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        public FileItem()
        {

        }

        public FileItem(BlobItem item)
        {
            this.Id = ConvertPathToId(item.Path);
            this.SearchContent = item.SearchContent;
            this.Directory = item.GetParentPath();
            this.LastModified = item.LastWriteTime ?? item.LastModified; //.HasValue ? (DateTime?)item.LastModified.Value.DateTime : null;
            this.Owner = item.Owner;
            this.Title = item.Title;
            this.Remark = item.Remark;
            this.Type = item is BlobDirectory ? "Directory" : "File";
            this.Path = EncodeFileUrl(item.Path);
            this.Length = item.Length;
            this.OriginalUrl = item.GetOriginalUrl();

            var file = item as BlobFile;
            if (file != null)
            {
                this.CheckoutUser = file.CheckOutUserId;
                //this.FullTextEntryDate = file.FullTextEntryDate;
            }
        }

        internal static string  EncodeFileUrl(string url)
        {
            return string.Join("/", url.Split('/').Select(x => Uri.EscapeDataString(x)));
            
        }

        internal static string ConvertPathToId(string path)
        {
            return path.Replace("/", "|").Replace(".", "*");
            //return StringCompressor.CompressString(path).Trim('=');
        }

        internal static string ConvertIdToPath(string id)
        {
            return id.Replace("|", "/").Replace("*", ".");
            //return StringCompressor.DecompressString(id);
        }
    }
}
